/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.data.resolver.ParamResolver;
import com.pig4cloud.pigx.system.constant.SystemParamNameConstant;
import com.pig4cloud.pigx.user.entity.UserApi;
import com.pig4cloud.pigx.user.service.UserApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户 api 表
 *
 * @author pigx code generator
 * @date 2022-11-29 17:24:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user_api")
@Api(value = "userapi", tags = "用户 api 表管理")
public class UserApiController {

    private final UserApiService userApiService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param userApi 用户 api 表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('user_userapi_view')")
    public R getUserApiPage(Page page, UserApi userApi) {
        Page<UserApi> userApiPage = userApiService.page(page, new QueryWrapper<UserApi>() {{
            if (userApi.getUserId() != null) {
                like("user_id", userApi.getUserId());
            }
        }});
        userApiPage.getRecords().forEach(e -> {
            e.setPassword(null);
        });
        return R.ok(userApiPage);
    }

    /**
     * 用户绑定API
     *
     * @param api api
     * @return r
     */
    @PostMapping("/binding")
    public R<String> userBindingApi(@RequestBody UserApi api) {
        return userApiService.userBindingApi(api);
    }

    /**
     * 解除绑定
     *
     * @param id 主键
     * @return r
     */
    @DeleteMapping("/binding")
    public R<String> relieveBindingApi(@RequestParam("id") Long id) {
        return userApiService.relieveBindingApi(id);
    }

    /**
     * 编辑api
     *
     * @param userApi api
     * @return r
     */
    @PutMapping
    public R<String> updateApi(@RequestBody UserApi userApi) {
        return userApiService.updateApi(userApi);
    }

    /**
     * 获取用户api列表
     *
     * @return r
     */
    @GetMapping("/list")
    public R<List<UserApi>> getApiList() {
        return userApiService.getApiList();
    }

}
